﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark
{
    public partial class LandingPage : System.Web.UI.Page
    {
        protected void Page_PreRender(object sender, EventArgs e)
        {
                Dictionary<long, SAN_PHAM> Cart = Session["Cart"] as Dictionary<long, SAN_PHAM>;
                DonHang.InnerHtml = GenerateCartHtml(Cart);
                Session["Cart"] = new Dictionary<long, SAN_PHAM>();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] == null)
                DangKyTaiKhoan.Style.Add("display", "block");
        }

        private string GenerateCartHtml(Dictionary<long, SAN_PHAM> Cart)
        {
            StringBuilder str = new StringBuilder();
            EMartEntities db = new EMartEntities();
            long? tt = 0;
            foreach (KeyValuePair<long, SAN_PHAM> item in Cart)
            {
                str.Append("<div class='row'>");
                str.Append("<div class='product'>");
                str.Append("<div class='col-md-2 col-sm-3 hidden-xs p-wr'>");
                str.Append("<div class='product-attrb-wr'>");
                str.Append("<div class='product-attrb'>");
                str.Append("<div class='image'> <a class='img' href='ChiTietSP.aspx?ID=" + item.Key + "'><img alt='product info' src='images/products/" + db.HINH_SAN_PHAM.Where(s => s.IDSanPham == item.Key).First().Hinh + "' title='product title'></a> </div>");
                str.Append("</div>");
                str.Append("</div>");
                str.Append("</div>");
                str.Append("<div class='col-md-5 col-sm-7 col-xs-9 p-wr'>");
                str.Append("<div class='product-attrb-wr'>");
                str.Append("<div class='product-meta'>");
                str.Append("<div class='name'><h3><a href='ChiTietSP.aspx?ID=" + item.Key + "'>" + item.Value.TenSanPham + "</a></h3></div>");
                if (item.Value.NgayBatDau <= DateTime.Now && item.Value.NgayKetThuc >= DateTime.Now)
                {
                    long? thanhtien = item.Value.DonGia - ((item.Value.DonGia * item.Value.GiamGia) / 100);

                    string tien = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", thanhtien);
                    str.Append("<div class='price'><span class='price-new'>" + tien + "<span class='sym'>đ</span></span>");
                    str.Append(" <span class='price-old'>" + item.Value.DonGia + "<span class='sym'>đ</span></span> </div>");
                }
                else
                {
                    string tien = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", item.Value.DonGia);

                    str.Append("<div class='price'><span class='price-new'>" + tien + "<span class='sym'>đ</span></span></div>");
                }

                str.Append("</div></div></div>");
                str.Append("<div class='col-md-2 hidden-sm hidden-xs p-wr'>");
                str.Append("<div class='product-attrb-wr'>");
                str.Append("<div class='product-attrb'>");
                str.Append("<div class='qtyinput'>");
                str.Append("<div class='quantity-inp'>");
                str.Append("<input disabled='disabled' type='text' id='" + item.Key + "' data-id='" + item.Key + "' class='quantity-input' name='p_quantity' value='" + item.Value.SoLuong + "'>");
                str.Append("</div></div></div></div></div>");
                str.Append("<div class='col-md-3 hidden-sm hidden-xs p-wr'>");
                str.Append("<div class='product-attrb-wr'>");
                str.Append("<div class='product-attrb'>");
                if (item.Value.NgayBatDau <= DateTime.Now && item.Value.NgayKetThuc >= DateTime.Now)
                {
                    long? thanhtien = (item.Value.DonGia - ((item.Value.DonGia * item.Value.GiamGia) / 100)) * item.Value.SoLuong;
                    tt += thanhtien;
                    string tien = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", thanhtien);
                    str.Append("<div class='price'> <span id='p" + item.Key + "' class='t-price'>" + tien + "</span><span class='sym t-price'>đ</span></div>");
                }
                else
                {
                    string tien = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", item.Value.DonGia * item.Value.SoLuong);
                    tt += item.Value.DonGia * item.Value.SoLuong;
                    str.Append("<div class='price'> <span id='p" + item.Key + "' class='t-price'>" + tien + "</span><span class='sym t-price'>đ</span> </div>");
                }

                str.Append("</div></div></div>");
                str.Append("</div></div>");
                str.Append("<div class='row clearfix f-space10'></div>");
            }
            
            return str.ToString();
        }
    }
}